package store.lunangangster.engine.bpmn.parser.handler;

import store.lunangangster.bpmn.model.Event;
import store.lunangangster.bpmn.model.StartEvent;
import store.lunangangster.bpmn.model.SubProcess;
import store.lunangangster.bpmn.model.definition.MessageEventDefinition;
import store.lunangangster.engine.bpmn.parser.BpmnParser;
import store.lunangangster.engine.impl.bpmn.behavior.NoneStartEventBehavior;

public class StartEventParseHandler extends EventParseHandler<StartEvent> {

  @Override
  public Class<? extends Event> getType() {
    return StartEvent.class;
  }

  @Override
  public void handle0(BpmnParser bpmnParser, StartEvent startEvent) {

    if (startEvent.getEventDefinitions() == null || startEvent.getEventDefinitions().isEmpty()) {
      startEvent.setBehavior(new NoneStartEventBehavior());
    } else if (startEvent.getParentContainer() instanceof SubProcess) {
       if (startEvent.getEventDefinitions().get(0) instanceof MessageEventDefinition) {
        bpmnParser.getBpmnParseHandlers().handleElement(bpmnParser, startEvent.getEventDefinitions()
            .get(0));
      }
    } else if (startEvent.getEventDefinitions().get(0) instanceof MessageEventDefinition) {
      bpmnParser.getBpmnParseHandlers().handleElement(bpmnParser, startEvent.getEventDefinitions()
          .get(0));
    }

    if (startEvent.getParentContainer().getInitialElement() == null) {
      startEvent.getParentContainer().setInitialElement(startEvent);
    }
  }

}
